package ioperations;
import book.Book;
import book.BookList;
import java.util.Scanner;

public class AddOperation implements IOPeration {
    public void work(BookList bookList){
        System.out.println("新增图书");
        Scanner scanner=new Scanner(System.in);
        System.out.println("请输入书名");
        String name= scanner.next();
        System.out.println("请输入作者的姓名");
        String auther=scanner.next();
        System.out.println("书的价格");
        int price=scanner.nextInt();
        System.out.println("请输入书的类型");
        String type= scanner.next();

        Book newbook=new Book(name,auther,price,type);
        int currentSize= bookList.getUsedSize();

        if(currentSize==bookList.getBooks().length){
            System.out.println("书架满了不能放书了");
            return;
        }

        for (int i = 0; i <currentSize ; i++) {
            Book book=bookList.getBook(i);
            if(book.getName().equals(name)){
                System.out.println("书名已存在不能添加了");
                return;
            }
        }
        bookList.setBook(currentSize,newbook);
        bookList.setUsedSize(currentSize+1);
        System.out.println("新增图书成功");
        return;
    }
}
